/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package commonClasses.Messages;

import commonClasses.*;
import commonClasses.Messages.Msg;
import commonClasses.commonInterfaces.MsgIntf;
import commonClasses.commonInterfaces.ProtocolIntf;

/**
 *
 * @author MGL
 */
public class LastItemMsg extends Msg implements MsgIntf{



    private Item item = null;

    public LastItemMsg(ProtocolIntf protocol) {
        super(protocol, MagicNumbers.LAST_ITEM_MSG_NR);
    }

    public LastItemMsg(ProtocolIntf protocol, Item item) {
        super(protocol, MagicNumbers.LAST_ITEM_MSG_NR);
        this.item = item;
    }

    public void receive() throws Exception {

        long itemID = receiveUint();
        Utils.clientReceived(itemID);

        long priceValue = receiveUint();
        Utils.clientReceived(priceValue);

        String priceCurrency = String.valueOf(receiveCharArr(3));
        Utils.clientReceived(priceCurrency);

        int length = longToInt(receiveUint());
        Utils.clientReceived(length);

        String desc = String.valueOf(receiveCharArr(length));
        Utils.clientReceived(desc);

        this.item = new Item();
        item.setBasic(itemID, new Price(priceCurrency, priceValue), desc);
    }

    public void send() throws Exception {

        if(item == null)
            throw new Exception("item nie zostal zainicjalizowany");

        int mesgId[] = this.idToIntArr();
        sendIntArr(mesgId);

        int itemId[] = longToArr(item.getId());
        sendIntArr(itemId);

        int priceValue[] = longToArr(item.getPrize().getValue());
        sendIntArr(priceValue);

        char priceCurrent[] = stringToCharArr(item.getPrize().getCurrent());
        sendCharArr(priceCurrent);

        int length[] = longToArr(item.getDescription().length());
        sendIntArr(length);

        char desc[] = stringToCharArr(item.getDescription());
        sendCharArr(desc, item.getDescription().length());
        


    }

    public Item getItem() {
        return item;
    }

}
